﻿#include"manager.h"
#include"operation.h"
#include"sort.h"
void manager(Sys sys)
{
	char account[20] = {};
	char password[20] = {};
	printf("请输入管理员账号：");
	scanf("%s", account);
	while (getchar() != '\n');
	printf("请输入管理员密码：");
	scanf("%s", password);
	while (getchar() != '\n');

	char ch = '\0';
	char RealAccount[20] = { "123456" };//测试密码与账户
	char RealPsd[20] = { "123456" };
	if (0 == strcmp(account, RealAccount) && 0 == strcmp(password, RealPsd))
	{
		printf("登录成功\n");
		system("pause");
		system("cls");
	}
	else
		return;
	system("pause");
	system("cls");
	while (1)
	{
		system("pause");
		system("cls");
		printf("---------------------1、添加航班----------------------\n");
		printf("---------------------2、删除航班----------------------\n");
		printf("---------------------3、航班信息----------------------\n");
		printf("---------------------4、客户信息----------------------\n");
		printf("---------------------5、退出管理员操作端--------------\n");
		printf("请输入要进行的操作：");
		int num = 0;
		scanf("%d", &num);
		switch (num)
		{
		case 1:
			addFlight(sys);
			break;
		case 2:
			deleteFlight(sys);
			break;
		case 3:
			system("pause");
			system("cls");
			checkLine(sys);
			break;
		case 4:
			checkCustomer(sys);
			break;
		case 5:
			return;
		}
	}

}

void addFlight(Sys sys)
{
	char beg[20] = {};
	char dest[20] = {};
	printf("请输入您想要添加的航班的起点：");
	scanf("%s", beg);
	while (getchar() != '\n');
	printf("请输入您想要添加的航班的终点：");
	scanf("%s", dest);
	while (getchar() != '\n');
	int i = 0;
	bool flag = false;
	for (i = 0; i < 3; i++)
	{
		if (0 == strcmp(sys->airline[i].beg, beg) && 0 == strcmp(sys->airline[i].dest, dest))
		{
			flag = true;
			break;
		}
	}
	if (!flag)
	{
		printf("没有找到该航线\n");
		return;
	}
	pFlight pNew = (pFlight)malloc(sizeof(Flight));
	memset(pNew, 0, sizeof(Flight));
	pNew->next = sys->airline[i].flights;
	sys->airline[i].flights = pNew;
	printf("请分别输入该航班的编号，飞机号,起飞日期,总乘客数：");
	int num, plane, date, tnum;
	scanf("%d%d%d%d", &num, &plane, &date, &tnum);
	while (getchar() != '\n');
	pNew->number = num;
	pNew->planeID = plane;
	pNew->startDay = date;
	pNew->leftNum = pNew->totalNum = tnum;
	pNew->level[0].leftNum = pNew->level[1].leftNum = tnum / 3;//三个舱位的座位平分
	pNew->level[0].totalNum = pNew->level[1].totalNum = tnum / 3;
	pNew->level[2].leftNum = pNew->level[2].totalNum = tnum - 2 * pNew->level[0].totalNum;
	printf("添加成功\n");
	sys->airline[i].flights = sortFlight(sys->airline[i].flights);
}

void deleteFlight(Sys sys)
{
	printf("请输入您想删除的航班号：");
	int num;
	scanf("%d", &num);
	while (getchar() != '\n');
	int flag = false;
	for (int i = 0; i < AllLine; i++)
	{
		pFlight pos = sys->airline[i].flights;
		pFlight pre = NULL;
		while (pos)
		{
			if (pos->number == num)
			{
				flag = true;
				printf("航班%d起点：%s,终点：%s,飞机编号：%d,起飞日期：星期%d,剩余一等舱票数：%d，剩余二等舱票数：%d，剩余三等舱票数：%d\n",
					pos->number, sys->airline[i].beg, sys->airline[i].dest, sys->airline[i].flights, pos->planeID, pos->startDay, pos->level[0].leftNum, pos->level[1].leftNum, pos->level[2].leftNum);
				printf("是否删除该航班？（Y/N）");
				char ch;
				scanf("%c", &ch);
				if (ch == 'Y' || ch == 'y')
				{
					if (pre == NULL)
					{
						sys->airline[i].flights = pos->next;
					}
					else
						pre->next = pos->next;
					free(pos);
					printf("已删除该航班。");
				}
				return;
			}
			pre = pos;
			pos = pos->next;
		}
	}
	if (!flag)
	{
		printf("没有找到该航班\n");
	}
}

void checkCustomer(Sys sys)
{
	printf("请选择：1、显示某航班所有已订购机票的客户信息\n");
	printf("2、查询单个客户信息\n");
	int flag;
	scanf("%d", &flag);
	while (getchar() != '\n');
	if (flag == 1)showAll(sys);
	else if (flag == 2)
	{
		checksigle(sys);
	}

}


void checksigle(Sys sys)
{
	printf("请输入要查询的客户身份证号码：");
	char idCard[20] = {};
	scanf("%s", idCard);
	while (getchar() != '\n');
	bool isfind = false;
	for (int i = 0; i < AllLine; i++)
	{
		pFlight pos = sys->airline[i].flights;
		while (pos)
		{
			BookedList temp = pos->booked;
			while (temp && strcmp(temp->idCard, idCard))
				temp = temp->next;
			if (temp)
			{
				isfind = true;
				printf("姓名：%s,身份证号：%s,订购%d张%d等票\n", temp->name, temp->idCard, temp->TicketNum, temp->level);
				return;
			}
			pos = pos->next;
		}
	}
	if (!isfind)
		printf("没有找到该客户信息\n");
}

void showAll(Sys sys)
{
	printf("请输入要查讯的航班号：");
	int num;
	scanf("%d", &num);
	int flag = false;
	for (int i = 0; i < AllLine; i++)
	{
		pFlight pos = sys->airline[i].flights;
		if (pos && num == pos->number)
		{
			flag = true;
			show(pos->booked);
			break;
		}
	}
	if (!flag)
	{
		printf("该航班尚未开通或还没有顾客订购机票\n");
	}
}

